// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        speed : 1, //拖动速度
        cameNode : { //摄像机
            default : null,
            type : cc.Node,
        },
        // popPrefab : { //代码添加预制体 显示层
        //     default : null,
        //     type : cc.Prefab,
        // },

        snowParticle:{ //雪花粒子特效
            default : null,
            type : cc.ParticleSystem,
        },
        popPrefab : { //拖动添加预制体 js单独定制  显示层
            default : null,
            type : cc.Node,
        },
    },

    onLoad () {
        //console.log("farm --onLoad")
        this.node.on(cc.Node.EventType.TOUCH_START, function(e){
            this.popPrefab.active = false
            //console.log("开始触摸"+e)
        }, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, function(e){
            var lx = e.getDeltaX()
            var ly = e.getDeltaY()
            var x = this.cameNode.x
            var y = this.cameNode.y
            //cc.log("触摸移动"+"cameNode.x"+this.cameNode.x+"cameNode.y"+this.cameNode.y);
            if(x - lx * this.speed<(1500-375)/2 && x -lx * this.speed>-(1500-375)/2){
                this.cameNode.x -= lx * this.speed
            }
            if(y - ly * this.speed<0 && y - ly * this.speed>-0){
                this.cameNode.y -= ly * this.speed
            }
        }, this);

        this.node.on(cc.Node.EventType.TOUCH_END, function(e){
            //cc.log("触摸结束");
        }, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, function(e){
            //cc.log("触摸取消");
        }, this);

      //this.createPopPrefab()
       this.initPreFab()
    },

    // createPopPrefab(){
    //     this.popPrefab = cc.instantiate(this.popPrefab)
    //     this.cameNode.addChild(this.popPrefab)
    //     this.popPrefab.zIndex = 1000
    //     this.popPrefab.active = false
    // },
    initPreFab(){ //初始化操作框
        this.popPrefab.zIndex = 1000
        this.popPrefab.active = false
    },

    handleAction(event){
        console.log("点击鹤")
        var tgnode = event.target;
        let map = this.node.getComponent(cc.Sprite);
        var tx = event.getLocationX();
        var ty = event.getLocationY();
        var pnode2 = map.node.convertToNodeSpaceAR(cc.v2(tx,ty));
        this.popPrefab.x = pnode2.x
        this.popPrefab.y = pnode2.y -80

        console.log("pnode2:X"+pnode2.x+"pnode2:Y"+pnode2.y)
        this.popPrefab.active = !this.popPrefab.active
    },


    start () {
        console.log("farm --start")
    },

    update (dt) {

    },

    snowAction(){
        this.snowParticle.resetSystem()
    },

    sunshineAction(){
        this.snowParticle.stopSystem()
    },
});
